//
//  FHComposePIctureView.swift
//  WeiBo
//
//  Created by qfh on 16/7/18.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit


//  重用标记
private let FHComposePictureViewCellIdentifier = "FHComposePictureViewCellIdentifier"

class FHComposePIctureView: UICollectionView {
    
    
    /// 执行删除闭包
    
     var addButtonClosure: (()->())?
    
    
    /// 图片数组
    lazy var imageArray: [UIImage] = [UIImage]()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
       let flowLayout = UICollectionViewFlowLayout()
      super.init(frame: frame, collectionViewLayout: flowLayout)
        
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        
        backgroundColor = UIColor.grayColor()
    
        delegate = self
        
        dataSource = self
        
        registerClass(FHComposePictureCollectionViewCell.self, forCellWithReuseIdentifier: FHComposePictureViewCellIdentifier)
        
        
        hidden = true
        
    }
    
    /**
     添加图片
     
     - parameter image: <#image description#>
     */
    func addImage(image: UIImage) {
        
        if imageArray.count < 9 {
        
            imageArray.append(image)
            
            reloadData()
        }
    
        
    
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //  每项之间的间距
        let itemMargin: CGFloat = 5
        
       let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        
        
        let itemW = (width - 2 * itemMargin) / 3
        
        flowLayout.itemSize = CGSize(width: itemW, height: itemW)
        
        flowLayout.minimumLineSpacing = itemMargin
        
        flowLayout.minimumInteritemSpacing = itemMargin
        
        
    }
    

}

// MARK: - UICollectionViewDelegate,UICollectionViewDataSource


extension FHComposePIctureView: UICollectionViewDelegate,UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if imageArray.count == 0 || imageArray.count == 9 {
        
             return imageArray.count
            
        }
        
         return imageArray.count + 1
        
       
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(FHComposePictureViewCellIdentifier, forIndexPath: indexPath) as! FHComposePictureCollectionViewCell
        
        if (imageArray.count == indexPath.item) {
        
            cell.image = nil
            
        
        } else {
        
            cell.image = imageArray[indexPath.item]
            
            cell.deleteButtonClosure = {
                
                self.imageArray.removeAtIndex(indexPath.item)
                
                self.reloadData()
                
                if self.imageArray.count == 0 {
                    
                    self.hidden = true
                    
                }
                
            }

        }
        
        
        
    
        
        return cell
        
    }
    
    
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.item == imageArray.count {
        
            addButtonClosure?()

        }

    }
    
    
    
    
}






class FHComposePictureCollectionViewCell: UICollectionViewCell {

//    图片
    var image: UIImage? {
    
        didSet {
        
            if image == nil {
            
                deleteButton.hidden = true
                
                //  加号按钮显示
                imageView.image = UIImage(named: "compose_pic_add")
               
                //  设置高亮图片
                imageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
            
            } else {
            
                imageView.image = image

                deleteButton.hidden = false
            
            }
        
        }
    
    
    }
    
    
 /// 执行删除闭包
    
    private var deleteButtonClosure: (()->())?
    
//    删除按钮
    
    private lazy var deleteButton: UIButton =  {
    
        let button = UIButton()
        
        button.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        
        button.addTarget(self, action: "deleteButtonAction", forControlEvents: .TouchUpInside)
    
    
    
    
        return button
    
    }()
    
    /// 图片view
    private lazy var imageView: UIImageView  = {
    
        let imageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
    
        return imageView
        
    
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



    func setUpUi() {
    
        
        contentView.addSubview(imageView)
        
        contentView.addSubview(deleteButton)
    
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
            
        }
        
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(contentView)
            make.top.equalTo(contentView)
        }
        
        
        
       
        
        
        
    }
    
    
    
    
    
    @objc private func deleteButtonAction() {
    
    

        deleteButtonClosure?()
    
    
    
    }
    
    
    
    // MARK: - 接收通知  接收键盘传过来的数据
    
    
    
    
    
    
    

}

















